import React, {Component} from 'react';
import {Icon, Input, Button} from 'antd';

import utils from '../../utils';

import './index.bundle.less';

export default class Index extends Component {
    handleSubmit = (e) => {
        let username = this.refs.username.refs.input.value;
        let data = utils.post('/api/login', {username}, '');
        data.then((res) => {
            if (res && res.code !== -1) {
                utils.removeCookie('token');
                utils.setCookie('token', res.token);
                location.pathname = '/system';
            } else {
                alert('用户名错误');
            }
        });
    }
    render() {
        return (
            <div className='login'>
                <div className='tooltip'>
                    <div className='title'>
                        <span>Linuxer</span>
                    </div>
                    <Input
                        ref='username'
                        className='user'
                        placeholder="Enter your username"
                        prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
                    />
                    <Button type="primary" htmlType="submit" className="loginbtn" onClick={this.handleSubmit}>
                        Log in
                    </Button>
                </div>
            </div>
        );
    }
}
